////////////////////////////////////////////////////////////////////////////////
//	Copyright (c) 2004 by Michael 'Astar' Sotnikov
//	stari4ek at gmail dot com
////////////////////////////////////////////////////////////////////////////////

#include "aiFilter.hpp"

#include <fstream>
#include <string>

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>

#include <boost/archive/xml_oarchive.hpp>

#include <boost/serialization/nvp.hpp>

namespace ai
{
	const std::string FILTERS_EXT = ".flt";

//////////////////////////////////////////////////////////////////////////
const char* FiltersModule_c::m_name			= "ai_filters";
const char* FiltersModule_c::m_prereqs[]	= { NULL };

bool FiltersModule_c::ModuleInit()
{
	// TODO: scan current path for perceptions
	boost::filesystem::directory_iterator it_dirend;	// end by default
	static const char * START_DIR = "."; 
	for( boost::filesystem::directory_iterator it_dir( START_DIR ); it_dir != it_dirend; ++it_dir )
	{
		// TODO: recursive directory's scanning
		if( !boost::filesystem::is_directory( *it_dir ) )
		{
			const std::string fname = it_dir->leaf();
			if( fname.substr( fname.length() - FILTERS_EXT.length() ) == FILTERS_EXT )	// is it our file?
			{
				std::ifstream f( fname.c_str() );

				boost::archive::xml_iarchive ar( f );
				FilterInfo_s filter;
				ar & boost::serialization::make_nvp( "aifilter", filter );
			}
		}
	}

	return Module_c::ModuleInit();
}

bool FiltersModule_c::ModuleDone()
{
	// TODO release all resources/memory
	return Module_c::ModuleDone();
}

} // ai
